$(function() {
	//确认密码失去焦点，判断是否与密码一致
	$('input[name="repassword"]').blur(function() {
		var password = $('input[name="password"]').val();
		var repassword = $(this).val();
		if (repassword != password) {
			flag = false;
			layer.tips('密码与确认密码不一致', 'input[name="repassword"]');
		}
	});

	//用户名输入框失去焦点，判断用户名是否重复
	$('input[name="userName"]').blur(function() {
		var userName = $(this).val();
		if (userName != '') {
			$.ajax({
				type: "GET",
				url: "http://127.0.0.1:8080/user/findUserByUserName",
				dataType: "json",
				data: {
					"userName": userName
				},
				success: function(data) {
					var code = data.code;
					if (code != 'success') {
						layer.tips(data.msg, 'input[name="userName"]');
					}
				}
			});
		} else {
			layer.tips('请输入用户名', this);
		}
	});

	//注册
	$(".submit").on("click", function() {
		var userName = $('input[name="userName"]').val();
		var password = $('input[name="password"]').val();
		var repassword = $('input[name="repassword"]').val();
		var phone = $('input[name="phone"]').val();
		var yanzhengma = $('input[name="yanzhengma"]').val();
		var flag = true;
		if (userName == '') {
			flag = false;
			layer.tips('请输入用户名', 'input[name="userName"]');
		}
		if (password == '') {
			flag = false;
			layer.tips('请输入密码', 'input[name="password"]', {
				tipsMore: true,
			});
		}
		if (repassword != password) {
			flag = false;
			layer.tips('密码与确认密码不一致', 'input[name="repassword"]', {
				tipsMore: true,
			});
		}
		if (phone == '') {
			flag = false;
			layer.tips('请输入手机号', 'input[name="phone"]', {
				tipsMore: true,
			});
		}
		if (yanzhengma == '') {
			flag = false;
			layer.tips('请输入验证码', 'input[name="yanzhengma"]', {
				tipsMore: true,
			});
		}
		if (!flag) {
			return;
		}
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/user/register",
			dataType: "json",
			data: {
				"userName": userName,
				"password": password,
				"phone": phone,
				"repassword": repassword
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') { //如果登录成功，跳转到登录界面
					window.location.href = './login.html';
				} else {
					layer.msg(data.msg);
				}
			}
		});
	});
});
